package com.koron.car.web.authorized.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.car.web.authorized.bean.entity.DelimitApply;
import com.koron.car.web.authorized.bean.query.DelimitApplyQuery;
import com.koron.car.web.authorized.bean.vo.DelimitApplyVO;
import com.koron.car.web.authorized.filter.CarDelimitFilter;
import com.koron.car.web.authorized.service.DelimitApplyService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (WCmDelimitApply)表控制层
 *
 * @author twcao
 * @since 2022-03-31 11:24:56
 */
@Slf4j
@RestController
@RequestMapping("/delimit/apply")
@Api(tags = "车辆编制-定编申请")
public class DelimitApplyController implements WorkflowController {

    /**
     * 服务对象
     */
    @Resource
    private DelimitApplyService delimitApplyService;

    @Autowired
    private CarDelimitFilter filter;

    @GetMapping("/selectOne")
    @ApiOperation("根据id查询信息")
    @ApiImplicitParam(name = "id", value = "概况id", required = true)
    public Response<DelimitApply> selectOne(@RequestParam String id) {
        log.info("根据id查询信息， id = {}", id);
        DelimitApply bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "queryById", DelimitApply.class, id);
        return Response.ok(bean);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或更新申请信息")
    public Response<DelimitApply> saveOrUpdate(@RequestBody DelimitApplyVO da) {
        log.info("新增或更新申请信息， da = {}", JSON.toJSONString(da));
        DelimitApply bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "saveOrUpdate", DelimitApply.class, da);
        return Response.ok(bean);
    }

    @GetMapping("/queryList")
    @ApiOperation("分页查询")
    public Response<PageInfo<DelimitApply>> queryList(DelimitApplyQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        log.info("分页查询， query = {}", JSON.toJSONString(query));
        List<DelimitApply> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除申请信息")
    public Response<Boolean> delete(@RequestParam String id) {
        log.info("根据id删除申请信息， id = {}", id);
        Boolean flag = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "deleteById", Boolean.class, id);
        return Response.ok(flag);
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        DelimitApplyVO bean = JSON.parseObject(redirectQuery.getFormVariables(), DelimitApplyVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitApplyService, "auditFinish", DelimitApplyVO.class, bean);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("定编申请审批设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("定编申请审批选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }
}
